home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / RANDOM.ASM < prev    next >
Assembly Source File  |  1993-05-04  |  3KB  |  92 lines

  1. ;********************************************;
  2. ; WASM Random Number Generator               ;
  3. ; By Eric Tauck                              ;
  4. ;                                            ;
  5. ; Defines:                                   ;
  6. ;                                            ;
  7. ;   RndSed   seed the number generator       ;
  8. ;   RndNum   return a random number          ;
  9. ;   RndNumN  return a random number 0 to N-1 ;
  10. ;                                            ;
  11. ; Note: Algorithm is based on the one pub-   ;
  12. ; lished in March 1987 Byte magazine         ;
  13. ;********************************************;
  14.  
  15. ;--- seed generator, uses time, month, and date
  16.  
  17.         mov     ah, 2CH         ;get time function
  18.         int     21H             ;execute
  19.         push    dx
  20.         push    cx
  21.         mov     ah, 2AH         ;get date
  22.         int     21H             ;execute
  23.         pop     bx
  24.         pop     ax
  25.         call    RndSed          ;seed generator
  26.         jmps    _random_end
  27.  
  28. ;--- data
  29.  
  30. _rnd_count      EQU     3                       ;number of generators
  31. _rnd_data       DW      0, 253, 65497           ;first generator data
  32.                 DW      0, 254, 65519           ;second generator data
  33.                 DW      0, 255, 65521           ;third gernerator data
  34.  
  35. ;================================================
  36. ; Seed the random number generator.  Note: the
  37. ; generator is automatically seeded.  Use this
  38. ; routine if wish a reproduceable sequence of
  39. ; random numbers.
  40. ;
  41. ; In: AX,BX,CX= three 16 bit seeds.
  42.  
  43. RndSed  PROC    NEAR
  44.         mov     _rnd_data, ax
  45.         mov     _rnd_data+6, bx
  46.         mov     _rnd_data+12, cx
  47.         ret
  48.         ENDP
  49.  
  50. ;================================================
  51. ; Generate a 16 bit random number.
  52. ;
  53. ; Out: AX= random number.
  54.  
  55. RndNum  PROC    NEAR
  56.         push    si
  57.         sub     bx, bx                  ;sum of components
  58.         mov     cx, _rnd_count          ;number of components
  59.         mov     si, OFFSET _rnd_data    ;start of data
  60.  
  61. _rdnum1 mov     ax, [si]                ;get seed
  62.         mul     ax, [si+2]              ;times multiplier
  63.         div     ax, [si+4]              ;divide by prime
  64.         mov     [si], dx                ;save remaider for next seed
  65.         add     bx, dx                  ;add in component
  66.         add     si, 6                   ;next entry
  67.         loop    _rdnum1                 ;loop for each component
  68.  
  69.         mov     ax, bx
  70.         pop     si
  71.         ret
  72.         ENDP
  73.  
  74. ;================================================
  75. ; Generate a random number from 0 to n-1.
  76. ;
  77. ; In: AX= max random number + 1 (i.e. 'n').
  78. ;
  79. ; Out: AX= random number (0 <= AX < in_AX).
  80.  
  81. RndNumN PROC    NEAR
  82.         push    ax
  83.         call    RndNum          ;get random number
  84.         pop     bx
  85.         sub     dx, dx          ;zero for divide
  86.         div     ax, bx          ;divide
  87.         mov     ax, dx          ;return modulus
  88.         ret
  89.         ENDP
  90.  
  91. _random_end
  92.